<?php
      session_start();
      error_reporting(0);
if (!isset($_SESSION["login"])) {
  header("Location: index.php");
}

$idRestaurant = $_GET["id"];
$idUser = $_SESSION["id"];


include("includes/header.php");
$startDate = date("Y-m-d");

$sql = "SELECT * FROM tb_reviews WHERE id_user = '".$idUser."' AND id_restaurant = '".$idRestaurant."'";
$query = mysql_query($sql);
$banyak = mysql_num_rows($query);
if ($banyak > 0) {
?>
  <script type="text/javascript">
    location.href = "restaurant.php?id="+<?php echo $idRestaurant; ?>;
  </script>
<?php
}

?>
  <?php
    //fungsi selected menu
        function selectedMenu($judul){
          $file = $_SERVER['PHP_SELF'];
            $info = pathinfo($file);
              $file_name =  basename($file,'.'.$info['extension']);
        $menu = explode('-',$file_name);
          if($menu[0] == $judul){
            echo "class='active'";
          }
        }
?>

<style type="text/css">
  
  .restoFeature {
    list-style: none;
    margin-left: 0;
  }
  .restoFeature li {
    float: left;
    margin-left: 5px;
  }
  .restoFeature li>img {
    border: 2px solid transparent;
    color: #fff;
    border-radius: 15px;
    padding-bottom: 3px;
    padding-left: 4px;
    padding-right: 4px;
  }
  .restoFeature li>img:hover {
    background-color: #eee;
    border: 2px solid #906090;
  }
  a:hover {
    text-decoration: none;
  }
  .twoCollumns {
      list-style: none;
      margin-left: 0;
  }
  .twoCollumns li {
      float: left;
      width: 50%;
  }
</style>
<div class="modal hide fade" id="modalDishes">
    <div class="modal-header">
        <a class="close" data-dismiss="modal">&times;</a>
        <h3>Recommended Dish</h3>
    </div>
    <div class="modal-body">
        <p>
            <ul class="twoCollumns">
                <?php
                    $sql = "SELECT * FROM tb_masakan_restaurant a
                            LEFT JOIN tb_dishes b ON b.id = a.id_dish
                            WHERE a.id_restaurant = '".$_GET["id"]."'";
                    $query = mysql_query($sql);
                    $i = 1;
                    while ($baris = mysql_fetch_array($query)) {
                ?>
                <li><label for="dish_<?php echo $i; ?>"><input id="dish_<?php echo $i; ?>" class="selDish" type="checkbox" value="<?php echo $baris["id"]; ?>"> <?php echo $baris["nama"]; ?></label></li>
                <?php
                        $i++;
                    }
                ?>
            </ul>
        </p>
    </div>
    <div class="modal-footer">
        <a href="#" class="btn" data-dismiss="modal">Close</a>
    </div>
</div>
    <div class="row-fluid">
<!-- Content -->
<?php
  $sql = "SELECT * FROM tb_restaurants WHERE id_resto = '".$_GET["id"]."'";
  $query = mysql_query($sql);
  $baris = mysql_fetch_array($query);
?>
<div class="span9">
  <div class="media">
    <div class="pull-left">
      <img class="media-object" data-src="holder.js/64x64" alt="64x64" style="width: 64px; height: 64px;" src="">
    </div>
    <div class="media-body">
      <h4 class="media-heading"><?php echo $baris["nama"]; ?></h4>
      <p><?php echo $baris["alamat"] ?></p>
      <p>
        <ul class="restoFeature">
          <li><?php echo getLabel($baris["is_outdoor"], "outdoor", "Outdoor"); ?></li>
          <li><?php echo getLabel($baris["is_wifi"], "wifi", "Wi-Fi"); ?></li>
          <li><?php echo getLabel($baris["is_parking"], "parking", "Parking Lot"); ?></li>
          <li><?php echo getLabel($baris["is_halal"], "halal", "Halal"); ?></li>
          <li><?php echo getLabel($baris["is_reservation"], "reserve", "Reservation"); ?></li>
          <li><?php echo getLabel($baris["is_delivery"], "delivery", "Delivery"); ?></li>
        </ul>
      </p>
    </div>
  </div>
<div>
  <form id="formReview" class="form-horizontal">
    <fieldset>
      <legend>Tulis review</legend>
      <div class="control-group">
        <label class="control-label" for="judul">Judul Review</label>
        <div class="controls">
          <input type="text" class="input-xlarge" id="judul">
        </div>
      </div>
      <div class="control-group">
        <label class="control-label" for="tanggal">Tanggal Kunjungan</label>
        <div class="controls">
          <input type="text" id="tanggal" name="tanggal" class="input-medium datePicker">
        </div>
      </div>
      <div class="control-group">
        <label class="control-label" for="review">Isi Review</label>
        <div class="controls">
          <textarea id="review" class="tc-review"></textarea>
        </div>
      </div>
      <div class="control-group">
        <label class="control-label" for="acara">Acara</label>
        <div class="controls">
          <select id="acara">
            <option>---pilih acara---</option>
            <?php
              $sql = "SELECT * FROM tb_occassion";
              $query = mysql_query($sql);
              while ($baris = mysql_fetch_array($query)) {
            ?>
              <option value="<?php echo $baris["id"]; ?>"><?php echo $baris["nama"]; ?></option>
            <?php
              }
            ?>
          </select>
        </div>
      </div>
      <div class="control-group">
        <label class="control-label" for="perHead">Perkepala</label>
        <div class="controls">
          <input type="text" class="input-xlarge" id="perHead">
        </div>
      </div>
      <div class="control-group">
        <label class="control-label" for="perHead">Masakan Rekomendasi</label>
        <div class="controls">
          <a class="btn" data-toggle="modal" href="#modalDishes">Pilih Masakan</a>
        </div>
      </div>
      <div class="control-group">
        <label class="control-label" for="textarea">Rasa</label>
        <div class="controls">
          <?php
            for ($i=1; $i <= 5 ; $i++) { 
          ?>
          <a href="#" class="rating"><i class="icon-star-empty icon-3x nostar"></i></a>
          <?php
            }
          ?>
          <input type="hidden" id="ratingRasa" class="ratingValue" value="0">
        </div>
      </div>
      <div class="control-group">
        <label class="control-label" for="textarea">Lingkungan</label>
        <div class="controls">
          <?php
            for ($i=1; $i <= 5 ; $i++) { 
          ?>
          <a href="#" class="rating"><i class="icon-star-empty icon-3x nostar"></i></a>
          <?php
            }
          ?>
          <input type="hidden" id="ratingLingkungan" class="ratingValue" value="0">
        </div>
      </div>
      <div class="control-group">
        <label class="control-label" for="textarea">Layanan</label>
        <div class="controls">
          <?php
            for ($i=1; $i <= 5 ; $i++) { 
          ?>
          <a href="#" class="rating"><i class="icon-star-empty icon-3x nostar"></i></a>
          <?php
            }
          ?>
          <input type="hidden" id="ratingLayanan" class="ratingValue" value="0">
        </div>
      </div>
      <div class="control-group">
        <label class="control-label" for="textarea">Kebersihan</label>
        <div class="controls">
          <?php
            for ($i=1; $i <= 5 ; $i++) { 
          ?>
          <a href="#" class="rating"><i class="icon-star-empty icon-3x nostar"></i></a>
          <?php
            }
          ?>
          <input type="hidden" id="ratingKebersihan" class="ratingValue" value="0">
        </div>
      </div>
      <div class="control-group">
        <label class="control-label" for="textarea">Harga</label>
        <div class="controls">
          <?php
            for ($i=1; $i <= 5 ; $i++) { 
          ?>
          <a href="#" class="rating"><i class="icon-star-empty icon-3x nostar"></i></a>
          <?php
            }
          ?>
          <input type="hidden" id="ratingHarga" class="ratingValue" value="0">
        </div>
      </div>
      <span id='msg-detail'></span>
      <div class="form-actions">
        <button type="submit" class="btn btn-primary">Write This</button>
        <button class="btn">Cancel</button>
      </div>
    </fieldset>
  </form>

  <div id="modalPlacer">
    
  </div>

  <div class="modal hide fade in" id="modalConfGambar" data-backdrop="static" data-keyboard="false">
    <div class="modal-header">
      <h3>Konfirmasi Upload Gambar</h3>
    </div>
    <div class="modal-body">
      <p>
        Anda ingin upload gambar untuk review ini?
        <button id="yesGambar" class="btn btn-primary">Iya</button>
        <button class="btn btn-primary noGambar">Tidak</button>
      </p>
    </div>
  </div>

</div>

<?php
$sql = "SELECT * FROM tb_restaurants";
$query = mysql_query($sql);
?>
      
  </div>
</div>
<!-- End Of Content -->

<?php
include("includes/footer.php");
?>
<!-- Local Javascipt -->
<script src="js/ckeditor/ckeditor.js"></script>
<script src="js/ckeditor/adapters/jquery.js"></script>
<script type="text/javascript">
var dishArr = [];
$('document').ready(function(){
  //alert("");
  var idReview;
  $('.tc-review').each(function(index, el) {
    var that = $(this);
    $(this).ckeditor(function() {
        var editor = that.ckeditorGet();
        // console.log(editor);

          editor.document.on('keyup', function(event) {
            /* do stuff */
            // validateNews();
        });
    },{
    toolbar: [
      { name: 'clipboard', groups: [ 'clipboard', 'undo' ], items: [ 'Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo' ] },
      { name: 'basicstyles', groups: [ 'basicstyles', 'cleanup' ], items: [ 'Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript', '-', 'RemoveFormat' ] },
      { name: 'tools', items: [ 'Maximize', 'ShowBlocks' ] },
      { name: 'paragraph', groups: [ 'list', 'indent', 'blocks', 'align', 'bidi' ], items: [ 'NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'Blockquote', '-', 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock', '-', 'BidiLtr', 'BidiRtl' ] },
      '/',
      { name: 'styles', items: [ 'Styles', 'Format', 'Font', 'FontSize' ] },
      { name: 'colors', items: [ 'TextColor', 'BGColor' ] },
      { name: 'others', items: [ '-' ] }
    ]
    });
  });

  $('.datePicker').datepicker({
    changeYear:'true',
    changeMonth:'true',
    yearRange:'1945:+2',
    dateFormat: 'yy-mm-dd',
    maxDate: '<?php echo $startDate; ?>'
  });

  $('#modalDishes').on('click', '.selDish', function(event) {
      // event.preventDefault();
      /* Act on the event */
    var that = $(this);
    if (that.is(":checked")) {
      dishArr.push(that.val());
    } else {
      var index = dishArr.indexOf(that.val());
      if (index > -1) {
          dishArr.splice(index, 1);
      }
    }
  });

  $('#formReview').on({
      click: function() {
          var that = $(this);
          var index = that.index();
          that.closest('.controls').find('a').find("i").removeClass('icon-star').addClass('icon-star-empty').addClass('nostar');
          that.closest('.controls').find('a').slice(0, that.index()+1).find("i").each(function(index, el) {
            $(this).removeClass('icon-star-empty').removeClass('nostar').addClass('icon-star').addClass('star'+(index+1));
          });
          that.closest('.controls').find('.ratingValue').val(index+1);
          // that.closest('.controls').find('a').slice(0, that.index()+1).find("i").removeClass('icon-star-empty').addClass('icon-star');
          $('.rating').stop().unbind('mouseleave');
          return false;
      }
      // mouseenter: function() {
      //     var that = $(this);
      //     // $(this).find('.action').stop().fadeIn();
      // },
      // mouseleave: function() {
      //     var that = $(this);
      //     that.closest('.controls').find('a').find("i").removeClass('icon-star').addClass('icon-star-empty');
      //     console.log("hahha");
      // }
  }, '.rating');

  $('#formReview').submit(function(event) {
    /* Act on the event */
    var dataReview = {
      id: <?php echo $_GET["id"]; ?>,
      judul: $('#judul').val(),
      tanggal: $('#tanggal').val(),
      review: $('#review').val(),
      acara: $('#acara').val(),
      perHead: $('#perHead').val(),
      recomendDish: dishArr,
      ratingRasa: $('#ratingRasa').val(),
      ratingLingkungan: $('#ratingLingkungan').val(),
      ratingLayanan: $('#ratingLayanan').val(),
      ratingKebersihan: $('#ratingKebersihan').val(),
      ratingHarga: $('#ratingHarga').val()
    };

    $.ajax({
      url: 'act-crud.php?act=review_write',
      type: 'POST',
      dataType: 'json',
      data: dataReview,
    })
    .done(function(data) {
      idReview = data.id;
      var message = "<div class='alert "+data.errorTipe+"'>"+
                      "<button type='button' class='close' data-dismiss='alert'>×</button>"+
                      data.msg+
                      "</div>";
      if (data.error == 1) {
          $('#msg-detail').html(message);
      }else{
          $('#msg-detail').html(message);
          $('#modalConfGambar').modal("show");
          setTimeout(function(){
              // location.href = "restaurant.php?id=<?php echo $_GET["id"]; ?>";
          }, 1000);
      }
      console.log("success");
    })
    .fail(function() {
      console.log("error");
    })
    .always(function() {
      console.log("complete");
    });
    return false;
  });

$('#yesGambar').click(function(event) {
  /* Act on the event */
  event.preventDefault();
  $('#modalConfGambar').modal("hide");
  $.ajax({
    url: 'modal-upload-gambar.php',
    type: 'POST',
    dataType: 'html',
    data: {id: idReview, idRestaurant: <?php echo $_GET["id"]; ?>},
  })
  .done(function(data) {
    $('#modalPlacer').html(data);
    console.log("success");
  })
  .fail(function() {
    console.log("error");
  })
  .always(function() {
    console.log("complete");
  });
  
});


  $('#modalConfGambar').on('click', '.noGambar', function(event) {
    /* Act on the event */
    // event.preventDefault();
    $.ajax({
      url: 'act-crud.php?act=update_status_review',
      type: 'POST',
      dataType: 'json',
      data: {idReview: idReview},
    })
    .done(function(data) {
      if (data.error == 1) {
      }else{
          setTimeout(function(){
              location.href = "restaurant.php?id=<?php echo $_GET["id"]; ?>";
          }, 1000);
      }
      console.log("success");
    })
    .fail(function() {
      console.log("error");
    })
    .always(function() {
      console.log("complete");
    });
    
  });

});
</script>
<!-- End Of Local Javascipt -->
</body>
</html>